package enterprises

import grails.test.*

class ProviderTests extends GrailsUnitTestCase {
	Provider provider
	TypeProvider typeProvider = new TypeProvider(name:"type_provider") 
	
    protected void setUp() {
        super.setUp()
		provider = new Provider()
		provider.name = "asd"
		provider.primaryProduct = "asd"	
		provider.typeProvider = typeProvider	
		mockForConstraintsTests(Provider, [provider])			
    }

    protected void tearDown() {
        super.tearDown()		
    }

    void testNullableTypeProvider() {		
		provider.typeProvider = null						
		assertFalse provider.validate()
		assertEquals "nullable", provider.errors["typeProvider"]
    }
	
	void testEmptyName() {
		provider.name = ""
		assertFalse provider.validate()
		assertEquals "blank", provider.errors["name"]			
	}
	
	void testSmallName() {
		provider.name = "as"
		assertFalse provider.validate()		
	}
	
	void testSmallPrimaryProduct() {
		provider.primaryProduct = "as"
		assertFalse provider.validate()
		assertEquals "minSize", provider.errors["primaryProduct"]
	}
	
	void testUniqueName() {
		mockDomain (Provider,
			[new Provider(name:"pr1", primaryProduct:"pr1", typeProvider:typeProvider),
			 new Provider(name:"pr2", primaryProduct:"pr2", typeProvider:typeProvider)])
		provider.name = "pr1"
		assertFalse provider.validate()
		assertEquals "unique", provider.errors["name"]
	}
}
